#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: RoutesManager.cs
//

#endregion

using System.Collections;
using System.Web.Routing;
using Ids.Web.Listing;
using Ids.Utils;


namespace Novagoal.Website.WebApp.Routing
{
    public static class RoutesManager
    {
        #region enums
        public enum RouteValues
        {
            [Value("id")]
            Id,
            [Value("sort_direction")]
            SortDirection,
            [Value("page_number")]
            PageNumber,
            [Value("sort_field")]
            SortField,
            [Value("page_size")]
            PageSize,
            [Value("action")]
            Action
        }
        #endregion

        /// <summary>
        /// Gets the sort link.
        /// </summary>
        /// <param name="wrapper">The wrapper.</param>
        /// <param name="sortField">the sort field</param>
        /// <param name="actionName">the action name</param>
        /// <returns></returns>
        public static RouteValueDictionary GetSortRouteValues(this ListWrapper wrapper, object sortField, string actionName)
        {
            string dir, sort;
            const int PAGE = 1;
            const int PAGE_SIZE = 10;

            if (wrapper.SortField.Equals(sortField))
            {
                //Invert Direction
                sort = wrapper.SortField.ToStringValue();
                dir = wrapper.SortDirection == SortDirectionType.Ascending
                          ? SortDirectionType.Descending.ToStringValue()
                          : SortDirectionType.Ascending.ToStringValue();
            }
            else
            {
                sort = sortField.ToStringValue();
                dir = SortDirectionType.Ascending.ToStringValue();
            }


            var routes = CreateRouteDictionary(PAGE_SIZE, sort, dir, PAGE, wrapper, actionName);
            return routes;
        }

        /// <summary>
        /// Creates the route dictionary with no action.
        /// </summary>
        /// <param name="pageSize">The page size</param>
        /// <param name="sort">The sort.</param>
        /// <param name="dir">The dir.</param>
        /// <param name="page">The page.</param>
        /// <param name="wrapper">The wrapper.</param>
        /// <returns></returns>
        private static RouteValueDictionary CreateRouteDictionary(int pageSize, string sort, string dir, int page, ListWrapper wrapper)
        {
            return CreateRouteDictionary(pageSize, sort, dir, page, wrapper, string.Empty);
        }

        /// <summary>
        /// Creates the route dictionary
        /// </summary>
        /// <param name="pageSize">The page size</param>
        /// <param name="sort">The sort.</param>
        /// <param name="dir">The dir.</param>
        /// <param name="page">The page.</param>
        /// <param name="wrapper">The wrapper.</param>
        /// <param name="actionName">The Action Name.</param>
        /// <returns></returns>
        private static RouteValueDictionary CreateRouteDictionary(int pageSize, string sort, string dir, int page, ListWrapper wrapper, string actionName)
        {
            RouteValueDictionary routes = new RouteValueDictionary
                                              {
                                                  { RouteValues.PageNumber.ToStringValue(), page }
                                              };

            if (!string.IsNullOrEmpty(sort))
                routes.Add(RouteValues.SortField.ToStringValue(), sort);

            routes.Add(RouteValues.PageSize.ToStringValue(), pageSize);

            if (!string.IsNullOrEmpty(dir))
                routes.Add(RouteValues.SortDirection.ToStringValue(), dir);

            if (!string.IsNullOrEmpty(actionName))
                routes.Add(RouteValues.Action.ToStringValue(), actionName);


            IDictionary filters = wrapper.Filters;
            if (filters != null)
            {
                foreach (object key in filters.Keys)
                    routes.Add(key.ToStringValue(), filters[key]);
            }

            return routes;
        }

        /// <summary>
        /// Gets the page route values.
        /// </summary>
        /// <param name="wrapper">The wrapper.</param>
        /// <param name="pageSize">The page Size</param>
        /// <param name="page">The page.</param>
        /// <returns></returns>
        public static RouteValueDictionary GetPageRouteValues(this ListWrapper wrapper, int pageSize, int page)
        {
            string sort = wrapper.SortField != null ? wrapper.SortField.ToStringValue() : null;
            string dir = wrapper.SortDirection.ToStringValue();

            var routes = CreateRouteDictionary(pageSize, sort, dir, page, wrapper);
            return routes;
        }

        /// <summary>
        /// Gets the page route values.
        /// </summary>
        /// <param name="wrapper">The wrapper.</param>
        /// <param name="pageSize">The page size</param>
        /// <returns></returns>
        public static RouteValueDictionary GetRouteValues(this ListWrapper wrapper, int pageSize)
        {
            var routes = CreateRouteDictionary(pageSize, wrapper.SortField.ToStringValue(), wrapper.SortDirection.ToStringValue(), wrapper.CurrentPage, wrapper);
            return routes;
        }
    }
}